package cn.tedu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.tedu.entity.User;
import cn.tedu.service.UserService;
import cn.tedu.service.impl.UserServiceImpl;
import cn.tedu.util.CommonValue;
import cn.tedu.util.PropertyUtil;
import cn.tedu.vo.Page;

/**
 * Servlet implementation class UserFindByPageServlet
 */
public class UserFindByPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserFindByPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取数据(pageSize currentPage  keywords)
		//从属性文件中获取
		int pageSize=Integer.parseInt(new PropertyUtil("page.properties").getPropertyValue("pageSize"));
	
		int currentPage=1;
		String currentPage_String=request.getParameter("currentPage");
		if(currentPage_String!=null) {
			currentPage=Integer.parseInt(currentPage_String);
		}
		
		String keyword1=request.getParameter("keyword1");
		String keyword2=request.getParameter("keyword2");
		
		String kw1=(keyword1==null)? "" : keyword1;
		String kw2=(keyword2==null)? "" : keyword2;
		String[] keywords=new String[] {kw1,kw2};
		
		//2.调用分页+模糊的业务
		UserService userService=new UserServiceImpl();
		Page<User> page=userService.findUserByPage(currentPage,pageSize,keywords);		
		System.out.println("page="+page);
		//3.把查询出来的结果转发给usershowbypage.jsp中
		//a.绑定page对象数据给request对象
		request.setAttribute("page", page);
		//b.转发reqeust对象到usershowbypage.jsp中
		request.getRequestDispatcher("usershowbypage.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
